<?
/**
* New menu template helpers
* I hope this helper could helps
* to accelerate new menu creation
*
* @author   adesanto_asman@yahoo.com
* @package  CMenuTemplate
*
*/

define('_FAILED_IF_EXISTS'                          , true);
define('_FILE_EXTENTION'                            , ".php");
define('_DIR_CONTROLLER'                            , "controllers");
define('_DIR_MODELS'                                , "models");
define('_DIR_VIEWS'                                 , "views");
define('_DIR_JAVASCRIPT'                            , "javascript");
define('_CONTROLLER_LOCAL'                          , "local.php");
define('_MODELS_FETCHERS'                           , "fetchers.php");
define('_MODELS_HANDLERS'                           , "handlers.php");
define('_JS_AJAX'                                   , "ajax.js");
define('_JS_MISC'                                   , "misc.js");
define('_LIST_ENTRY'                                , "entry.php");
define('_LIST_CAT_PICK'                             , "entry_category_pick.php");
define('_LIST_ADD_ROW'                              , "entry_add_row.php");
define('_LIST_DATA_BODY'                            , "entry_data_body.php");
define('_LIST_RESTORE'                              , "entry_restore_me.php");
define('_LIST_INPUT_HEADER'                         , "entry_header_html.php");
define('_LIST_INPUT_DETAIL'                         , "entry_detail_html.php");
define('_LIST_INPUT_CHECK'                          , "entry_details_check.php");
define('_LIST_INPUT_CONFIRM'                        , "entry_upload_confirm.php");
define('_LIST_INPUT_ENTRY_DB'                       , "upentrytodb.php");
define('_LIST_INPUT_MODIFY_DB'                      , "upmodifytodb.php");
define('_LIST_VIEW_EDIT_HDR'                        , "viewdboredit.php");
define('_LIST_VIEW_EDIT_VIEW'                       , "viewdboredit_view.php");
define('_LIST_VIEW_EDIT_MODIFY'                     , "viewdboredit_modify.php");
define('_LIST_VIEW_EDIT_APPROVE'                    , "viewdboredit_approve.php");
define('_LIST_VIEW_EDIT_CANCEL'                     , "viewdboredit_cancel.php");
define('_LIST_VIEW_EDIT_DELETE'                     , "viewdboredit_delete.php");
define('_DEFAULT_SLASHES'                           , "/");

define('_OPEN_CLOSE_BRACKET'                        , "<?


                                                       ?>");

/**
*
* @package CMenuTemplate
*
*/
class CMenuTemplate{

  public $basePath;

  public $dbTech;

  public $menuName;

  public $menuNumber;

  public $titleName;

  public $titleCaption;

  public $modulName;

  public $menuBodySrcDbHdr;

  public $menuBodySrcDbDtl;

  public $menuMstUserId;

  public $entSeqName;

  public $refSequence;

  function __construct($dbTech){

    $this->dbTech = $dbTech;
  }

  /**
  * build the tocmenu_XX.php
  *
  * @param  string  $basePath
  * @param  string  $menuName
  * @param  string  $menuNumber
  * @param  string  $titleName
  * @param  string  $titleCaption
  *
  */
  function buildMenuHead(&$basePath,
                         &$menuName,
                         &$menuNumber,
                         &$titleName,
                         &$titleCaption){

    $this->basePath     = $basePath;
    $this->menuName     = $menuName;
    $this->menuNumber   = $menuNumber;
    $this->titleName    = $titleName;
    $this->titleCaption = $titleCaption;

    $dbTech = &$this->dbTech;

    $CFile = new CGFile;

    require("CMenuTemplate/menuHead.php");

    /**
    * Build the file
    * and transfer the content
    *
    */
    $filename = $menuName. "_" .
                $menuNumber.
                _FILE_EXTENTION;

    if($CFile->createFile($basePath,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuHead)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }
  }

  /**
  * build the tocmenu_body_XX.php
  *
  * @param  string  $modulName
  * @param  string  $menuBodySrcDbHdr
  * @param  string  $menuBodySrcDbDtl
  *
  */
  function buildMenuBody(&$modulName,
                         &$menuBodySrcDbHdr,
                         &$menuBodySrcDbDtl){

    $basePath   = &$this->basePath;
    $menuName   = &$this->menuName;
    $menuNumber = &$this->menuNumber;
    $dbTech     = &$this->dbTech;

    $this->modulName      = $modulName;
    $this->menuBodySrcHdr = $menuBodySrcDbHdr;
    $this->menuBodySrcDtl = $menuBodySrcDbDtl;

    $CFile = new CGFile;

    require("CMenuTemplate/menuBody.php");

    /**
    * Build the file
    * and transfer the content
    *
    */
    $filename = $menuName. "_body_" .
                $menuNumber.
                _FILE_EXTENTION;

    if($CFile->createFile($basePath,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuBody)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }
  }

  /**
  * Build controllers directory and local.php
  *
  * @param  void
  *
  */
  function buildControllers(){

    $basePath   = &$this->basePath;

    $menuName   = &$this->menuName;
    $menuNumber = &$this->menuNumber;

    $CFile = new CGFile;

    $dirName = $basePath        .
               $menuName        . "_" .
               $menuNumber      .
               _DEFAULT_SLASHES ;

    /**
    * If the directory is exists
    * YES: continue
    * NO : Create new one
    *
    */
    $CFile->createDirectory($dirName);

    $dirName .= _DIR_CONTROLLER.
                _DEFAULT_SLASHES ;

    $CFile->createDirectory($dirName);

    require("CMenuTemplate/menuList/menuClassCol.php");

    $filename = _CONTROLLER_LOCAL;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      $CFile->writeFile($menuClassLocal);
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $CFile->close();
  }

  /**
  * Build models dir and fetchers.php and handlers.php
  *
  * @param void
  *
  */
  function buildModels(){

    $basePath   = &$this->basePath;

    $menuName   = &$this->menuName;
    $menuNumber = &$this->menuNumber;

    $CFile = new CGFile;

    $dirName = $basePath        .
               $menuName        . "_" .
               $menuNumber      .
               _DEFAULT_SLASHES ;

    /**
    * If the directory is exists
    * YES: continue
    * NO : Create new one
    *
    */
    $CFile->createDirectory($dirName);

    $dirName .= _DIR_MODELS.
                _DEFAULT_SLASHES ;

    $CFile->createDirectory($dirName);


    require("CMenuTemplate/menuList/menuClassCol.php");

    $filename = _MODELS_FETCHERS;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      $CFile->writeFile($menuClassFetchers);
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _MODELS_HANDLERS;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      $CFile->writeFile($menuClassHandlers);
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $CFile->close();
  }

  /**
  * Build views directory
  *
  * @param  void
  *
  */
  function buildViews(){

    $basePath   = &$this->basePath;

    $menuName   = &$this->menuName;
    $menuNumber = &$this->menuNumber;

    $CFile = new CGFile;

    $dirName = $basePath        .
               $menuName        . "_" .
               $menuNumber      .
               _DEFAULT_SLASHES ;

    /**
    * If the directory is exists
    * YES: continue
    * NO : Create new one
    *
    */
    $CFile->createDirectory($dirName);

    $dirName .= _DIR_VIEWS.
                _DEFAULT_SLASHES ;

    $CFile->createDirectory($dirName);

    $CFile->close();
  }

  /**
  * Build javascript directory
  * with ajax.js and misc.js
  *
  * @param  void
  *
  */
  function buildJavascript(){

    $basePath   = &$this->basePath;

    $menuName   = &$this->menuName;
    $menuNumber = &$this->menuNumber;

    $CFile = new CGFile;

    $dirName = $basePath        .
               $menuName        . "_" .
               $menuNumber      .
               _DEFAULT_SLASHES ;

    /**
    * If the directory is exists
    * YES: continue
    * NO : Create new one
    *
    */
    $CFile->createDirectory($dirName);

    $dirName .= _DIR_JAVASCRIPT.
                _DEFAULT_SLASHES ;

    $CFile->createDirectory($dirName);

    require("CMenuTemplate/menuList/menuJavascript.php");

    $filename = _JS_AJAX;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      $CFile->writeFile($menuAjax);
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _JS_MISC;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      $CFile->writeFile($menuJavascript);
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $CFile->close();
  }

  /**
  * build the entry.php
  *
  * @param  string  $menuMstUserId    source db of USER_ID
  *
  */
  function buildMenuEntry(&$menuMstUserId){

    $basePath   = &$this->basePath;
    $menuName   = &$this->menuName;
    $menuNumber = &$this->menuNumber;
    $modulName  = &$this->modulName;
    $dbTech     = &$this->dbTech;

    $menuBodySrcDbHdr = &$this->menuBodySrcHdr;
    $menuBodySrcDbDtl = &$this->menuBodySrcDtl;

    $this->menuMstUserId = $menuMstUserId;

    $CFile = new CGFile;

    require("CMenuTemplate/menuList/menuEntry.php");

    /**
    * Build the file
    * and transfer the content
    *
    */
    $dirName = $basePath        .
               $menuName        . "_" .
               $menuNumber      .
               _DEFAULT_SLASHES ;

    $filename = _LIST_ENTRY;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuEntry)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }
  }

  /**
  * build the category_pick.php
  *           entry_data_body.php
  *           entry_add_row.php
  *           entry_restore_me.php
  *
  * @param  void
  *
  */
  function buildMenuOptional(){

    $basePath   = &$this->basePath;
    $menuName   = &$this->menuName;
    $menuNumber = &$this->menuNumber;
    $modulName  = &$this->modulName;

    $CFile = new CGFile;

    require("CMenuTemplate/menuList/menuOptional.php");

    /**
    * Build the file
    * and transfer the content
    *
    */
    $dirName = $basePath        .
               $menuName        . "_" .
               $menuNumber      .
               _DEFAULT_SLASHES ;

    $filename = _LIST_CAT_PICK;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuCategoryPick)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_DATA_BODY;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuEntryDataBody)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_RESTORE;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuEntryRestore)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_ADD_ROW;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuEntryAddRow)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }
  }

  /**
  * build the entry_header_html.php
  *           entry_detail_html.php
  *
  * @param  void
  *
  */
  function buildMenuInputForm(){

    $basePath   = &$this->basePath;
    $menuName   = &$this->menuName;
    $menuNumber = &$this->menuNumber;
    $modulName  = &$this->modulName;
    $dbTech     = &$this->dbTech;

    $this->menMstUserId = $menuMstUserId;

    $CFile = new CGFile;

    require("CMenuTemplate/menuList/menuInputForm.php");

    /**
    * Build the file
    * and transfer the content
    *
    */
    $dirName = $basePath        .
               $menuName        . "_" .
               $menuNumber      .
               _DEFAULT_SLASHES ;

    $filename = _LIST_INPUT_HEADER;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuEntryHeader)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_INPUT_DETAIL;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuEntryDetail)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_INPUT_CONFIRM;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuEntryConfirm)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_INPUT_CHECK;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuEntryCheck)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }
  }

  /**
  * Build the DB uploader
  * upentrytodb.php and upmodifytodb.php
  *
  * @param  void
  *
  */
  function buildUploader(&$entSeqName){

    $basePath   = &$this->basePath;
    $menuName   = &$this->menuName;
    $menuNumber = &$this->menuNumber;
    $modulName  = &$this->modulName;
    $dbTech     = &$this->dbTech;

    $srcDbHdr   = &$this->menuBodySrcHdr;
    $srcDbDtl   = &$this->menuBodySrcDtl;

    $this->entSeqName = $entSeqName;

    $CFile = new CGFile;

    require("CMenuTemplate/menuList/menuDbUpload.php");

    /**
    * Build the file
    * and transfer the content
    *
    */
    $dirName = $basePath        .
               $menuName        . "_" .
               $menuNumber      .
               _DEFAULT_SLASHES ;

    $filename = _LIST_INPUT_ENTRY_DB;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuDbEntry)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_INPUT_MODIFY_DB;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuDbModify)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }
  }

  /**
  * Build the core
  * viewdboredit.php
  * viewdboredit_view.php
  * viewdboredit_modify.php
  * viewdboredit_approve.php
  * viewdboredit_cancel.php
  * viewdboredit_delete.php
  *
  * @param  void
  *
  */
  function buildCore($refSequence){

    $basePath   = &$this->basePath;
    $menuName   = &$this->menuName;
    $menuNumber = &$this->menuNumber;
    $modulName  = &$this->modulName;
    $dbTech     = &$this->dbTech;

    $srcDbHdr   = &$this->menuBodySrcHdr;
    $srcDbDtl   = &$this->menuBodySrcDtl;

    $menuBodySrcDbHdr = &$this->menuBodySrcHdr;
    $menuBodySrcDbDtl = &$this->menuBodySrcDtl;

    $this->refSequence = $refSequence;

    $CFile = new CGFile;

    require("CMenuTemplate/menuList/menuCore.php");

    /**
    * Build the file
    * and transfer the content
    *
    */
    $dirName = $basePath        .
               $menuName        . "_" .
               $menuNumber      .
               _DEFAULT_SLASHES ;

    $filename = _LIST_VIEW_EDIT_HDR;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuCoreHdr)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_VIEW_EDIT_VIEW;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuCoreView)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_VIEW_EDIT_MODIFY;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuCoreModify)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_VIEW_EDIT_APPROVE;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuCoreApprove)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_VIEW_EDIT_CANCEL;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuCoreCancel)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }

    $filename = _LIST_VIEW_EDIT_DELETE;

    if($CFile->createFile($dirName,
                          $filename,
                          _FAILED_IF_EXISTS)){

      if($CFile->writeFile($menuCoreDelete)){

        //Misc::usrDie("Notify: Write complete");
      }else{

        Misc::usrDie("Error: ".$filename." can not be write");
      }

      $CFile->close();
    }else{

      Misc::usrDie("Error: this ".$filename." already exists");
    }
  }
}

?>